/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package df.bean.process;

import df.bean.db.conn.DBConn;
import df.bean.obj.util.FindDate;
import java.sql.SQLException;

/**
 *
 * @author USER
 */
public class ProcressMatchTable {
    private String hpCode = "";
    private String mm = "";
    private String yyyy="";
    private String typeGuarantee = "";
    private static final int[] DAYS = { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };


    private String getHpCode() {
        return hpCode;
    }

    private String getMm() {
        return mm;
    }

    private String getYyyy() {
        return yyyy;
    }

    private void setHpCode(String hpCode) {
        this.hpCode = hpCode;
    }

    private void setMm(String mm) {
        this.mm = mm;
    }

    private void setYyyy(String yyyy) {
        this.yyyy = yyyy;
    }

    private String getTypeGuarantee() {
        return typeGuarantee;
    }

    private void setTypeGuarantee(String typeGuarantee) {
        this.typeGuarantee = typeGuarantee;
    }


    private boolean isValid(int m, int d, int y) {
        if (m < 1 || m > 12)      return false;
        if (d < 1 || d > DAYS[m]) return false;
        if (m == 2 && d == 29 && !isLeapYear(y)) return false;
        return true;
    }
    private boolean isLeapYear(int y) {
        if (y % 400 == 0) return true;
        if (y % 100 == 0) return false;
        return (y % 4 == 0);
    }
    private String next(String dt) {
        int day = Integer.parseInt(dt);
        int m = Integer.parseInt(this.getMm());
        int y = Integer.parseInt(this.getYyyy());
        
        if (isValid( m , day + 1, y)){
            return y +""+ (m < 10 ? "0" + m : m) + "" + ((day+1) < 10 ? "0" + (day+1) : (day+1));
        }
        else if (isValid(m + 1, 1, y)){
            return y +""+ ((m+1) < 10 ? "0" + (m+1) : (m+1)) + "01";
        }
        else return (y+1) +""+ "01" + "01";
    }

    private String StartDate(String dt){
        if(dt.length()==1) dt = "0" + dt;
        return this.getYyyy()+ "" + this.getMm() + "" + dt ;
    }
    
    private String EndDate(String starttime,String endtime, String date){
        String dt = this.getYyyy()+ "" + this.getMm() + "" + this.DAYS[Integer.parseInt(this.getMm())];
        if("DLY".equalsIgnoreCase(this.getTypeGuarantee()) || "MLY".equalsIgnoreCase(this.getTypeGuarantee())){
            dt = "" + Integer.parseInt(starttime) + " : " + Integer.parseInt(endtime);
            if(Integer.parseInt(starttime) > Integer.parseInt(endtime)){
                dt = this.next(date);
            }else{
                dt = this.StartDate(date);
            }
        }      
        return dt;
    }
    
    public boolean ProcessMain(String h,String m, String y){
        this.setHpCode(h);
        this.setMm(m);
        this.setYyyy(y);
        
        DBConn conn = new DBConn();
        FindDate fn = new FindDate();
        
        try {
            conn.setStatement();
        } catch (SQLException ex) {
        }

        String SQLCommand = "";
        SQLCommand = "";
        //System.out.println("Delete SQLCommand : " + SQLCommand);
        try{
            /*
             DLY -> DLY : 20090403120000
             MLY -> MLY : 20090403120000
             MLA -> MLY : 200904
             */
            String guarantee_code = "";            
            String select = "SELECT TT.HOSPITAL_CODE, TT.DOCTOR_CODE, TT.GUARANTEE_TYPE, TT.DAY," +
                    "TT.START_TIME, TT.STOP_TIME, TT.GUARANTEE_AMOUNT, TURN_AMOUNT, DR.GUARANTEE_SOURCE " +
                    "FROM DOCTOR_TIME_TABLE TT LEFT OUTER JOIN DOCTOR DR " +
                    "ON TT.HOSPITAL_CODE = DR.HOSPITAL_CODE AND TT.DOCTOR_CODE = DR.CODE " +
                    "WHERE TT.HOSPITAL_CODE='"+ this.getHpCode() +"'";
            String arr[][]= conn.query(select);
            String dt = "";
            String sqlInsert = "";
            for(int i=0; i < arr.length ; i++){
                dt = "";
                this.setTypeGuarantee(arr[i][2]);

                if(!"MLA".equalsIgnoreCase(this.getTypeGuarantee())){
                    dt = fn.FindDateAll(arr[i][3], this.getMm(), this.getYyyy());
                    String[] arrDt = dt.split("\\|");
                    guarantee_code = this.getYyyy() + "" + this.getMm();
                    for(int k = 0 ; k < arrDt.length ; k++){
                        sqlInsert = "INSERT INTO STP_GUARANTEE(HOSPITAL_CODE, USER_ID, MM, YYYY, " +
                        		"GUARANTEE_DR_CODE, GUARANTEE_CODE, ADMISSION_TYPE_CODE, " +
                        		"GUARANTEE_TYPE_CODE, START_DATE, START_TIME, END_DATE, " +
                        		"END_TIME, GUARANTEE_AMOUNT, GUARANTEE_EXCLUDE_AMOUNT, GUARANTEE_SOURCE) " +
                                "VALUES('"+ this.getHpCode() +"','TRANSFER','"+this.getMm()+"','"+
                                this.getYyyy() +"', '"+ arr[i][1] +"','"+ guarantee_code +"','U','"+
                                arr[i][2] +"','"+ this.StartDate(arrDt[k]) +"','"+arr[i][4]+"','"+
                                this.EndDate(arr[i][4], arr[i][5], arrDt[k]) +"','"+ arr[i][5] +"','"+
                                arr[i][6]+"','"+arr[i][7]+"','"+arr[i][8]+"')";
                        //System.out.println(sqlInsert);                        
                        conn.insert(sqlInsert);
                        //System.out.println(sqlInsert);
                    }
                }else{
                    dt = " MLY in STP_GUARANTEE ";
                    guarantee_code = this.getYyyy() + "" + this.getMm();
                    //System.out.print(dt + " = " + arr[i][2] + " -> " + guarantee_code + " : ");
                    //System.out.println(this.StartDate("01") +" : "+ this.EndDate(arr[i][4], arr[i][5], ""));
                    sqlInsert = "INSERT INTO STP_GUARANTEE(HOSPITAL_CODE, USER_ID, MM, YYYY, " +
                    		"GUARANTEE_DR_CODE, GUARANTEE_CODE, ADMISSION_TYPE_CODE, " +
                    		"GUARANTEE_TYPE_CODE, START_DATE, START_TIME, END_DATE, " +
                    		"END_TIME, GUARANTEE_AMOUNT, GUARANTEE_EXCLUDE_AMOUNT, GUARANTEE_SOURCE) " +
                            "VALUES('"+ this.getHpCode() +"','TRANSFER','"+this.getMm()+"','"+ 
                            this.getYyyy() +"', '"+ arr[i][1] +"','"+ guarantee_code +"','U'," +
                            "'MLY','"+ this.StartDate("01") +"','"+arr[i][4]+"','"+ 
                            this.EndDate(arr[i][4], arr[i][5], "") +"','"+ arr[i][5] +"','"+
                            arr[i][6]+"','"+arr[i][7]+"','"+arr[i][8]+"')";
                    //System.out.println(sqlInsert);
                    conn.insert(sqlInsert);
                }
                try{
                	conn.commitDB();
                }catch(Exception e){
                	System.out.println("Process Transfer timetable : "+sqlInsert + e);
                	conn.rollDB();
                }
            }

            conn.closeStatement("");
            conn.closeDB("");
            return true;
        }catch(Exception err){
            //conn.rollDB();
            //System.out.println(SQLCommand + err);
            return false;
        }finally{
        	
        }
    }
//    public static void main(String[] arg){
//        ProcressMatchTable p = new ProcressMatchTable();
//        p.ProcessMain("00001","04","2009");
//    }
}
